package cn.maven.utils;

import cn.maven.module.Column;
import cn.maven.module.Table;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TableUtil {
    private static final String DRIVER = PropertiesUtil.getValue("database.properties", "driver");
    private static final String URL = PropertiesUtil.getValue("database.properties", "url");
    private static final String USER = PropertiesUtil.getValue("database.properties", "user");
    private static final String PASSWORD = PropertiesUtil.getValue("database.properties", "password");

    private static Connection connection;
    private static DatabaseMetaData dbmd;

    static {
        //1、获取数据库连接
        try {
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
            //2、获取meteData
            dbmd = connection.getMetaData();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


   /* public static void getTables(){

        try {
            //获取所有表名结果集
            ResultSet rs = dbmd.getTables(null, null, null, new String[]{"TABLE"});
            //遍历结果集
            while (rs.next()){
                String tableName = rs.getString("TABLE_NAME");
                System.out.println(tableName);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }*/


/*
    //获取列名、数据类型、注释
    public static void getCloumns(String tableName){
        try {
            //得到列的结果集
            ResultSet rs = dbmd.getColumns(null, "%", tableName, "%");
            while (rs.next()) {
                //获取列名
                String columnsName = rs.getString("COLUMN_NAME");
                //数据类型
                String columnsType = rs.getString("TYPE_NAME");
                //注释
                String remarks = rs.getString("REMARKS");
                System.out.println(columnsName+columnsType+remarks);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
*/



    /**
     * 获取数据库表名
     */
    public static List<Table> getTable() {
        List<Table> tableList = new ArrayList<>();
        try {
            //获取所有表名结果集
            ResultSet rs = dbmd.getTables(null, null, null, new String[]{"TABLE"});
            //遍历结果集
            while (rs.next()) {
                Table table = new Table();
                String tableName = rs.getString("TABLE_NAME");
                //储存表名
                table.setTableName(tableName);
                //储存类名（需要把首字母大写，以及其中的‘_’等去除，然后匹配驼峰命名法）
                table.setClassName(StringUtil.getString(tableName));
                //储存小写类名
                table.setLowerClassName(StringUtil.lowerStr(StringUtil.getString(tableName)));
                //设置相应列属性
                table.setColumns(getColumns(tableName));
                tableList.add(table);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tableList;
    }

    /**
     * 获取列名、数据类型、注释
     *
     * @param tableName
     */
    public static List<Column> getColumns(String tableName) {
        List<Column> columnList = new ArrayList<>();
        try {
            ResultSet rs = dbmd.getColumns(null, "%", tableName, "%");
            while (rs.next()) {
                Column column = new Column();
                //获取列名
                String columnsName = rs.getString("COLUMN_NAME");
                //数据类型
                String columnsType = rs.getString("TYPE_NAME");
                //注释
                String remarks = rs.getString("REMARKS");
                //设置相应值
                column.setColumnName(columnsName);
                column.setColumnType(columnsType);
                column.setRemakes(remarks);

                column.setFiledName(StringUtil.lowerStr(StringUtil.getString(columnsName)));
                column.setJavaType(StringUtil.swtichType(columnsType));
                column.setUpperFiledName(StringUtil.getString(columnsName));
                columnList.add(column);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return columnList;
    }
}
